package cn.mesmile.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author zb
 * @date 2021/1/31 14:50
 * @Description: 自定义realm
 */
public class CustomRealm extends AuthorizingRealm {


    /**
     *  用户校验 【权限】 的时候会调用该方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println(">>>>>>>>>>>> 进入到授权");
        // 获取到身份信息
        Object primaryPrincipal = principalCollection.getPrimaryPrincipal();
        String username = (String) primaryPrincipal;
        // 模拟从数据库中获取到[权限]
        Set<String> permissions = getPermissionByDb(username);
        // 从数据获取【角色】
        Set<String> roles = getRolesByDb(username);

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        simpleAuthorizationInfo.setRoles(roles);

        return simpleAuthorizationInfo;
    }

    private Set<String> getRolesByDb(String username) {
        Map<String, Set<String>> stringSetHashMap = new HashMap<>();
        Set<String> objectsOne = new HashSet<>();
        Set<String> objectsTwo = new HashSet<>();
        objectsOne.add("role1");
        objectsOne.add("role2");

        objectsTwo.add("root");

        stringSetHashMap.put("jack", objectsOne);
        stringSetHashMap.put("xdclass", objectsTwo);

        return stringSetHashMap.get(username);

    }

    private Set<String> getPermissionByDb(String username) {
        Map<String, Set<String>> stringSetHashMap = new HashMap<>();
        Set<String> objectsOne = new HashSet<>();
        Set<String> objectsTwo = new HashSet<>();
        objectsOne.add("video:buy");
        objectsOne.add("video:find");

        objectsTwo.add("video:*");

        stringSetHashMap.put("jack", objectsOne);
        stringSetHashMap.put("xdclass", objectsTwo);

        return stringSetHashMap.get(username);
    }

    /**
     * 用户登录 的时候回调用此方法
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println(">>>>>>>>>>>> 进入到认证");
        // 身份信息
        Object principal = authenticationToken.getPrincipal();
        String username = (String) principal;
        // 模拟从数据库获取密码
        String pwd = getPasswordByDb(username);

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username,pwd,this.getName());


        return simpleAuthenticationInfo;
    }


    private String getPasswordByDb(String username) {
        HashMap<String, String> map = new HashMap<>();
        map.put("jack","123");
        map.put("xdclass","456");

        return map.get(username);
    }


}
